<?php

//帐户控制器
class Accounts extends Controller {

	function Accounts()
	{
		parent::Controller();	
		
		$this->load->model("accounts_model", "am");
		$this->load->model("site_model", "sm");
		$this->load->model("classes_model", "cm");
		$this->load->library("encrypt");
		$this->load->library("session");
		//登陆状态检测
		$this->userid = $this->am->is_logined();
		$this->account = $this->am->get_account();
	}
	
	//显示验证码
	function yzcode() 
	{
		$this->load->library("yzcode");
		$this->yzcode->outImg();
	}
	
	//检测验证码
	function check_yzcode($yzcode) 
	{
		session_start();
		if($yzcode != $_SESSION["yzcode"]) {
			return false;
		}
		return true;
	}
	
	//注册与登陆
	function index()
	{
		
		//检测登陆状态
		if($this->userid) {
			//show_failed("已登陆", "/");
			redirect("/");
		}
		
		//处理字符
		parse_str($_SERVER['QUERY_STRING'], $query_string);
		(isset($query_string['forget'])) ? $forget = 1 : $forget = 0;
		$viewdata['forget'] = $forget;
		
		//处理默认跳转返回地址
		if(isset($_SERVER["HTTP_REFERER"])) {
			$sessdata = array(
				"forward" => $_SERVER["HTTP_REFERER"],
			);
			$this->session->set_userdata($sessdata);
		}
		
		//处理指定跳转返回地址
		if(isset($query_string['forward'])) {
			//安全处理
			if($query_string['forward'] == 'accounts/opening') {//申请热站
				$sessdata = array(
					"forward" => gurl($query_string['forward']),
				);
				$this->session->set_userdata($sessdata);
			}
		}
		
		//获取背景广告图片
		$this->load->model("ads_model");
		$viewdata['ad'] = $this->ads_model->get(46);
		//输出公共header
		$this->autoload_model->output_header();
		
		$this->load->view(__CLASS__."/index", $viewdata);
		
		//输出公共footer
		$this->autoload_model->output_footer();
	}
	
	//申请开站介绍
	function open()
	{
		//检测登陆状态
		//if(!$this->userid) {
		//	show_failed("请先登陆", "accounts/");
		//}
		//输出公共header
		$this->autoload_model->output_header();
		
		$viewdata["account"] = $this->account;
		$this->load->view(__CLASS__."/open", $viewdata);
		
		//输出公共footer
		$this->autoload_model->output_footer();
		
	}
	
	//申请开张
	function opening()
	{
		//检测登陆状态
		if(!$this->userid) {
			show_failed("请先登陆", "accounts/");
		}
		
		//表单提交处理
		if($dosubmit = p("dosubmit")) {
			$step = p("step");
			switch($step) {
				case "1"://STEP1
					$sitedata = array(
						"userid" => $this->userid,
						"name" => p("name"),
						"domain" => p("domain"),
						"description" => p("description"),
						"classid" => p("subclass"),
					);
					$siteid = $this->sm->opening_step1($sitedata);
					if($siteid) {//提示成功进入STEP2
						//STEP2
//						$auth = $this->encrypt->encode(p("name")."_".$siteid);//auth rule
//						redirect("accounts/opening/".$auth);
						redirect("accounts/opening/".$siteid);
					} else {
						show_failed("申请失败");
					}
				break;
				case "2";//STEP2
					$sitedata = array(
						"realname" => p("realname"),
						"usermail" => p("usermail"),
						"phone" => p("phone"),
						"reason" => p("reason"),
						"siteid" => p("siteid"),
					);
					$result = $this->sm->opening_step2($sitedata, $this->userid, $this->account['account_username']);
					if($result) {
//						show_success("感谢你申请热闹网热站", "/");
						//输出公共header
						if(!isset($pagedata["title"])) $pagedata["title"] = "开轻站 | 热闹网";
						if(!isset($pagedata["keywords"])) $pagedata["keywords"] = "热闹、热闹网、热闹100、renao、renao100、轻网站、网站、小站、微网站、博客、社区、互动";
						if(!isset($pagedata["description"])) $pagedata["description"] = "热闹网(www.renao100.com) - 前所未有的互动轻网站社区。每个人可以为喜欢的事物建立一个轻型网站，并且允许共同维基创建内容。适合兴趣站、组织站甚至官方站。轻网站可以是WEB1.0，更可以是WEB2.0。";
						$this->autoload_model->output_header($pagedata);
						
						$this->load->view(__CLASS__."/opening_step3");
						
						//输出公共footer
						$this->autoload_model->output_footer();
					} else {
						show_failed("申请失败", "/");
					}
				break;
			}
			return;
		}
	
		//判断STEP
		if(!$auth = u(3)){//STEP1
			$this->__opening_step1();
			return;
		}
		//STEP2验证合法性
//		$auth = $this->encrypt->decode($auth);
		$siteid = $this->sm->check_openauth($auth);
		if(!$siteid) {
			show_failed("非法请求");
		}
		$this->__opening_step2($siteid);
	}
	
	//申请开张 STEP1
	function __opening_step1()
	{
		//检测是否开过好站
		$result = $this->sm->has_usersite($this->userid);
		if($result) show_failed("请不要重复申请", "/");
		
		//获取顶级分类
		$topclass = $this->cm->get_subclass();
		
		//输出公共header
		if(!isset($pagedata["title"])) $pagedata["title"] = "开轻站 | 热闹网";
		if(!isset($pagedata["keywords"])) $pagedata["keywords"] = "热闹、热闹网、热闹100、renao、renao100、轻网站、网站、小站、微网站、博客、社区、互动";
		if(!isset($pagedata["description"])) $pagedata["description"] = "热闹网(www.renao100.com) - 前所未有的互动轻网站社区。每个人可以为喜欢的事物建立一个轻型网站，并且允许共同维基创建内容。适合兴趣站、组织站甚至官方站。轻网站可以是WEB1.0，更可以是WEB2.0。";
		$this->autoload_model->output_header($pagedata);
		
		$viewdata["topclass"] = $topclass;
		$viewdata["account"] = $this->account;
		$this->load->view(__CLASS__."/opening_step1", $viewdata);
		
		//输出公共footer
		$this->autoload_model->output_footer();
	}
	
	//申请开张 STEP2
	function __opening_step2($siteid)
	{
		//输出公共header
		if(!isset($pagedata["title"])) $pagedata["title"] = "开轻站 | 热闹网";
		if(!isset($pagedata["keywords"])) $pagedata["keywords"] = "热闹、热闹网、热闹100、renao、renao100、轻网站、网站、小站、微网站、博客、社区、互动";
		if(!isset($pagedata["description"])) $pagedata["description"] = "热闹网(www.renao100.com) - 前所未有的互动轻网站社区。每个人可以为喜欢的事物建立一个轻型网站，并且允许共同维基创建内容。适合兴趣站、组织站甚至官方站。轻网站可以是WEB1.0，更可以是WEB2.0。";
		$this->autoload_model->output_header($pagedata);
		
		$viewdata["siteid"] = $siteid;
		$viewdata["account"] = $this->account;
		$this->load->view(__CLASS__."/opening_step2", $viewdata);
		
		//输出公共footer
		$this->autoload_model->output_footer();
	}
	
	//找回密码
	function forget()
	{
		//检测登陆状态
		if($logined = $this->am->is_logined()) {
			show_failed("已登陆", "/");
			
		}
		//表单提交处理
		if($dosubmit = p("dosubmit")) {
			$flag = $this->check_yzcode(p("yzcode"));
			if(!$flag) {
				show_failed("验证码错误");
			}
			$email = p("email");
			$result = $this->am->forget($email);
			if($result) {//提示成功
				//输出公共header
				$this->autoload_model->output_header();
				$this->load->view(__CLASS__."/forget_step2");
				//输出公共footer
				$this->autoload_model->output_footer();
				return ;
			} else {
				show_failed("操作失败");
			}
		}
		
		//输出公共header
		$this->autoload_model->output_header();
		$this->load->view(__CLASS__."/forget");
		//输出公共footer
		$this->autoload_model->output_footer();
	}
	
	//注册处理
	function register()
	{
		if($dosubmit = p("dosubmit")) {
			//验证码
			$flag = $this->check_yzcode(p("yzcode"));
			if(!$flag) {
				show_failed("验证码错误");
			}
			//邮件地址是否重复
			$exsits_email = $this->am->exsits_email(p("email"));
			$exsits_username = $this->am->exsits_username(p("username"));
			if($exsits_email || $exsits_username) {
				show_failed("Email或用户名已经被注册");
			}
			
			$regdata = array(
				"email" => p("email"),
				"username" => p("username"),
				"password" => p("password"),
			);
			$result = $this->am->register($regdata);
			if($result) {//提示成功
				//自动登陆
				$logindata = array(
					"email" => p("email"),
					"password" => p("password"),
				);
				$result = $this->am->login($logindata);
				if($result) {//提示成功
					//处理跳转链接
					$forward = $this->session->userdata("forward");
					if($forward != "" && strstr($forward, "www.renao100.com")) {
						$forwardurl = str_replace("http://www.renao100.com","",$forward);
						//清除跳转链接
						$sessdata = array(
							"forward" => "",
						);
						$this->session->unset_userdata($sessdata);
					} else {
						$forwardurl = "";
					}
					show_success("登陆成功", $forwardurl);
				} else {
					show_failed("登陆失败");
				}
			} else {
				show_failed("注册失败");
			}
			
		} 
	}
	
	//ajax登陆处理
	function ajaxlogin()
	{
		//处理跳转链接
		$forward = $this->session->userdata("forward");
		if($forward != "" && strstr($forward, "www.renao100.com")) {
			$forwardurl = str_replace("http://www.renao100.com","",$forward);
			//清除跳转链接
			$sessdata = array(
			"forward" => "",
			);
			$this->session->unset_userdata($sessdata);
		} else {
			$forwardurl = "";
		}
		show_success("登陆成功", $forwardurl);
	}
	
	//登陆处理
	function login()
	{
		if($dosubmit = p("logindosubmit")) {
			$logindata = array(
				"email" => p("loginemail"),
				"password" => p("loginpassword"),
			);
			if(p("forget")) {
				$forget = 1;
			} else {
				$forget = 0;
			}
			$result = $this->am->login($logindata, $forget);
			if($result) {//提示成功
				//处理跳转链接
				$forward = $this->session->userdata("forward");
				if($forward != "" && strstr($forward, "www.renao100.com")) {
					$forwardurl = str_replace("http://www.renao100.com","",$forward);
					//清除跳转链接
					$sessdata = array(
						"forward" => "",
					);
					$this->session->unset_userdata($sessdata);
				} else {
					$forwardurl = "";
				}
				show_success("登陆成功", $forwardurl);
			} else {
				show_failed("登陆失败");
			}
		}
		redirect("/");
	}
	
	//退出登陆
	function logout()
	{
		$this->am->logout();
		show_success("退出成功");
		
	}
	
	
}

/* End of file */